import 'dart:async';
import 'package:get/get.dart';
import 'splash_state.dart';
import '../../routes/app_pages.dart';

/// 闪屏页面逻辑控制器 - Splash Screen Logic Controller
///
/// 管理闪屏页面的倒计时和跳转逻辑
/// Manages splash screen countdown and navigation logic
class SplashLogic extends GetxController {
  final SplashState state = SplashState();
  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
    _startCountdown();
  }

  @override
  void onClose() {
    _timer?.cancel();
    super.onClose();
  }

  /// 开始倒计时 - Start Countdown
  void _startCountdown() {
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (state.countdown > 1) {
        state.countdown--;
        update();
      } else {
        _timer?.cancel();
        _navigateToHome();
      }
    });
  }

  /// 跳过闪屏 - Skip Splash
  void skipSplash() {
    _timer?.cancel();
    _navigateToHome();
  }

  /// 导航到主页 - Navigate to Home
  void _navigateToHome() {
    Get.offAllNamed(Routes.home);
  }
}